package feizhou.untils.tools;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.Map;
import java.util.Map.Entry;


/**
 * webservice工具类。
 */
public class WebServiceUtil {
    /**
     * webservice服务地址
     */
    private static String serv = "IP";

    /**
     * 拼接soap串。
     *
     * @param mothed 调用webservice的方法
     * @param param  调用参数
     * @return 返回soap串。
     */
    private static String getSoap(String mothed, Map<String, Object> param) {
        StringBuffer soap = new StringBuffer();
        soap.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
        soap.append("<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">");
        soap.append("<soap:Body>");

        soap.append("<").append(mothed).append(" xmlns=\"http://tempuri.org/\"").append(">");

        for (Entry<String, Object> entry : param.entrySet()) {
            soap.append("<").append(entry.getKey()).append(">");
            soap.append(entry.getValue().toString());
            soap.append("</").append(entry.getKey()).append(">");
        }

        soap.append("</").append(mothed).append(">");

        soap.append("</soap:Body>");
        soap.append("</soap:Envelope>");
        return soap.toString();
    }

    /**
     * 调用webservice接口。
     *
     * @param mothed 调用webservice的方法
     * @param param  调用参数
     * @return 返回调用结果。
     */
    public static String execute(String mothed, Map<String, Object> param) {
        String result = "";
        try {
            String soap = getSoap(mothed, param);

            URL url = new URL(serv);
            URLConnection conn = url.openConnection();
            conn.setUseCaches(false);
            conn.setDoInput(true);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", Integer.toString(soap.length()));
            conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            conn.setRequestProperty("SOAPAction", "http://tempuri.org/" + mothed);

            OutputStream os = conn.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
            osw.write(soap);
            osw.flush();
            osw.close();
            StringBuilder sTotalString = new StringBuilder();
            String sCurrentLine = "";
            InputStream is = conn.getInputStream();
            BufferedReader l_reader = new BufferedReader(new InputStreamReader(is));
            while ((sCurrentLine = l_reader.readLine()) != null) {
                sTotalString.append(sCurrentLine);
            }
            result = sTotalString.toString();
            result = result.replace("&lt;", "<").replace("&gt;", ">");
            result = result.substring(result.indexOf("root") - 1, result.lastIndexOf("root") + 5);
        } catch (Exception e) {
            e.printStackTrace();
            result = "<root><code>3</code><msg>出现异常</msg></root>";
        }
        return result;
    }
}